using System;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Entity
{
	/// <summary>
	/// Summary description for Version.
	/// </summary>
	public class VersionCode
	{
		public long code1 = 0;
		public long code2 = 0;
		public long code3 = 0;
		public long code4 = 0;

		public VersionCode( int c1, int c2, int c3, int c4 )
		{
			this.code1 = c1;
			this.code2 = c2;
			this.code3 = c3;
			this.code4 = c4;
		}
		
		public VersionCode( string versionCode ){
			StringTokenizer tokenizer = new StringTokenizer( versionCode, "." );
			try
			{
				if( tokenizer.Count == 1 )
				{
					this.code1 = System.Int64.Parse( tokenizer.NextToken() );
				}
				else if( tokenizer.Count == 2 )
				{
					this.code1 = System.Int64.Parse( tokenizer.NextToken() );
					this.code2 = System.Int64.Parse( tokenizer.NextToken() );
				}
				else if( tokenizer.Count == 3 )
				{
					this.code1 = System.Int64.Parse( tokenizer.NextToken() );
					this.code2 = System.Int64.Parse( tokenizer.NextToken() );
					this.code3 = System.Int64.Parse( tokenizer.NextToken() );
				}
				else if( tokenizer.Count == 4 )
				{
					this.code1 = System.Int64.Parse( tokenizer.NextToken() );
					this.code2 = System.Int64.Parse( tokenizer.NextToken() );
					this.code3 = System.Int64.Parse( tokenizer.NextToken() );
					this.code4 = System.Int64.Parse( tokenizer.NextToken() );
				}
				else
				{
					throw new InfoJetException( "Version format is not valid.", "VC01" );
				}
			}
			catch( Exception e )
			{
				throw new InfoJetException( "Version format is not valid.", "VC02" );
			}
		}

		public int compare( VersionCode versionCode ){
			if( this.code1 == versionCode.code1 &&
				this.code2 == versionCode.code2 &&
				this.code3 == versionCode.code3 &&
				this.code4 == versionCode.code4 ){
				return 0;
			}
			if( this.code1 < versionCode.code1 ){
				return -1;
			}else{
				if( this.code2 < versionCode.code2 ){
					return -1;
				}else{
					if( this.code3 < versionCode.code3 ){
						return -1;
					}else{
						if( this.code4 < versionCode.code4 ){
							return -1;
						}
					}
				}
			}
			return 1;
		}

		public string Value(){
			return code1 + "." + code2 + "." + code3 + "." + code4;
		}

	}
}
